export default {
  namespaced: true,
  state: () => ({
    data: [
      // {
      //   id: 1,
      //   title: '《产品经理手册》',
      //   price: 45,
      //   num: 1,
      //   checked: true,
      //   cover: 'product/2020-0820-5f3e17d6ed7e8.png',
      //   description: '产品设计 / 《产品经理手册》',
      //   collects_count: 3,
      //   cover_url: 'https://oss.shop.eduwork.cn/product/2020-0820-5f3e17d6ed7e8.png'
      // },
      // {
      //   id: 2,
      //   title: '《Linux私房菜》',
      //   price: 55,
      //   num: 2,
      //   checked: true,
      //   cover: 'product/2020-0820-5f3e177b79824.png',
      //   description: '云计算 / 《Linux私房菜》',
      //   collects_count: 5,
      //   cover_url: 'https://oss.shop.eduwork.cn/product/2020-0820-5f3e177b79824.png'
      // },
      // {
      //   id: 3,
      //   title: '《Linux命令大全》',
      //   price: 55,
      //   num: 3,
      //   checked: true,
      //   cover: 'product/2020-0820-5f3e1745869a5.png',
      //   description: '云计算 / 《Linux命令大全》',
      //   collects_count: 6,
      //   cover_url: 'https://oss.shop.eduwork.cn/product/2020-0820-5f3e1745869a5.png'
      // },
      // {
      //   id: 4,
      //   title: '《Java程序设计》',
      //   price: 108,
      //   num: 1,
      //   checked: true,
      //   cover: 'product/2020-0820-5f3e16f27f829.png',
      //   description: '后端 / 《Java程序设计》',
      //   collects_count: 1,
      //   cover_url: 'https://oss.shop.eduwork.cn/product/2020-0820-5f3e16f27f829.png'
      // },
      // {
      //   id: 5,
      //   title: '《Java核心编程》',
      //   price: 32,
      //   num: 2,
      //   checked: false,
      //   cover: 'product/2020-0820-5f3e16c2aba88.png',
      //   description: '后端 / 《Java核心编程》',
      //   collects_count: 1,
      //   cover_url: 'https://oss.shop.eduwork.cn/product/2020-0820-5f3e16c2aba88.png'
      // }
    ],
    data1: [],
    total: 0,
    allChecked: false // 是否全选
  }),
  mutations: {
    // 初始化数据
    init_data (state, items) {
      state.data = items
    },
    calc_m (state) {
      let data = 0
      state.data.forEach(n => {
        if (n.checked) data += n.num * n.price
      })
      state.total = data.toFixed(2) - 0
    },
    del_Data (state, index) {
      state.data.splice(index, 1)
      const allChecked = state.data.every(e => e.checked)
      state.allChecked = allChecked
    },
    // 更新数量
    Change_num (state, list) {
      state.data = list
    },
    // 单选(参数保持一致)
    single_checked (state, { index, checked }) {
      state.data[index].checked = checked
      const allChecked = state.data.every(e => e.checked)
      state.allChecked = allChecked
    },
    // 全选
    All_Checked (state, checked) {
      state.data.forEach(n => {
        n.checked = checked
      })
      state.allChecked = checked
    },
    add_Item (state, item) {
      state.data.unshift(item)
      const allChecked = state.data.every(e => e.checked)
      state.allChecked = allChecked
    },
    Update_num (state, { isAdd, num }) {
      // console.log(isAdd, num)
      state.data[isAdd].num += num
    },
    add_ItemLs (state, item) {
      state.data1.unshift(item)
      const allChecked = state.data.every(e => e.checked)
      state.allChecked = allChecked
    },
    delete_ItemLs (state) {
      state.data1 = []
    }
  }
}
